package com.niguang.work.api.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.niguang.common.constant.ResultCode;
import com.niguang.common.pojo.Result;
import com.niguang.common.util.ApiTokenUtils;
import com.niguang.work.api.entity.ReformApply;
import com.niguang.work.api.entity.ReformUser;
import com.niguang.work.api.enums.ReformRoleTypeEnum;
import com.niguang.work.api.mapper.ReformApplyMapper;
import com.niguang.work.api.service.ReformApplyService;
import com.niguang.work.api.service.ReformUserService;
import com.niguang.work.api.vo.ReformDetailListVo;
import com.niguang.work.api.vo.ReformDetailVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 'work.v_personSite' is not BASE TABLE 服务实现类
 * </p>
 *
 * @author 12380
 * @since 2018-09-06
 */
@Service
public class ReformApplyServiceImpl extends ServiceImpl<ReformApplyMapper, ReformApply> implements ReformApplyService {

    @Resource
    private ApiTokenUtils apiTokenUtils;

    @Resource
    private ReformUserService reformUserService;

    @Override
    public Result getApplyDetails(Long contentId) {
        ReformDetailVo reformDetail = baseMapper.getReformDetail(contentId);
        if (!ObjectUtils.isEmpty(reformDetail)) {
            String unionId = apiTokenUtils.getUnionId();
            EntityWrapper<ReformUser> entityWrapper = new EntityWrapper<>();
            entityWrapper.eq(ReformUser.UNION_ID, unionId).eq(ReformUser.SITE_ID, reformDetail.getSiteId());
            ReformUser reformUser = reformUserService.selectOne(entityWrapper);
            List<ReformDetailListVo> reformDetailList = baseMapper.getReformDetailList(contentId);
            if (!ObjectUtils.isEmpty(reformUser) && CollectionUtils.isNotEmpty(reformDetailList)) {
                List<ReformDetailListVo> collect = reformDetailList.parallelStream().filter(p ->
                        p.getValidFlag() == 0 && p.getAppointUserId() == null && p.getDealFlag() == 0).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(collect)) {
                    ReformDetailListVo reformDetailListVo = collect.parallelStream().sorted(Comparator.comparing(ReformDetailListVo::getCreateTime).reversed()).findFirst().get();
                    reformDetail.setDealFlag(reformDetailListVo.getDealFlag());
                    if (reformUser.getOpenId().equals(reformDetailListVo.getApplyUserId())) {
                        reformDetail.setShowButton(true);
                    }
                }
                reformDetail.setRoleType(reformUser.getRoleType());
                reformDetail.setRoleTypeText(ReformRoleTypeEnum.getName(reformUser.getRoleType()));
            }
            reformDetail.setList(reformDetailList);
        }
        return Result.build(ResultCode.SUCCESS, "工单详情查询成功", reformDetail);
    }

    @Override
    public Result getApplyDetailsHtml(Long contentId) {
        ReformDetailVo reformDetail = baseMapper.getReformDetail(contentId);
        if (!ObjectUtils.isEmpty(reformDetail)) {
            EntityWrapper<ReformUser> entityWrapper = new EntityWrapper<>();
            entityWrapper.eq(ReformUser.UNION_ID, reformDetail.getUnionId()).eq(ReformUser.SITE_ID, reformDetail.getSiteId());
            ReformUser reformUser = Optional.ofNullable(reformUserService.selectOne(entityWrapper)).orElseGet(ReformUser::new);
            List<ReformDetailListVo> reformDetailList = baseMapper.getReformDetailList(contentId);
            if (CollectionUtils.isNotEmpty(reformDetailList)) {
                List<ReformDetailListVo> collect = reformDetailList.parallelStream().filter(p ->
                        p.getValidFlag() == 0 && p.getAppointUserId() == null && p.getDealFlag() == 0).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(collect)) {
                    ReformDetailListVo reformDetailListVo = collect.parallelStream().sorted(Comparator.comparing(ReformDetailListVo::getCreateTime).reversed()).findFirst().get();
                    reformDetail.setDealFlag(reformDetailListVo.getDealFlag());
                }
                reformDetail.setRoleType(reformUser.getRoleType());
                reformDetail.setRoleTypeText(ReformRoleTypeEnum.getName(reformUser.getRoleType()));
            }
            reformDetail.setList(reformDetailList);
        }
        return Result.build(ResultCode.SUCCESS, "工单详情查询成功", reformDetail);
    }


}
